package com.kjsdfgk.hjmdsf.ftlive.mvp.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.kjsdfgk.hjmdsf.app.entity.Account;
import com.kjsdfgk.hjmdsf.app.utils.SharedPrefsUtils;
import com.kjsdfgk.hjmdsf.charge.mvp.ui.activity.MemberChargeActivity;
import com.kjsdfgk.hjmdsf.ftlive.di.component.DaggerLiveV3FtComponent;
import com.kjsdfgk.hjmdsf.ftlive.mvp.contract.LiveV3FtContract;
import com.kjsdfgk.hjmdsf.ftlive.mvp.presenter.LiveV3FtPresenter;
import com.kjsdfgk.hjmdsf.live.mvp.bean.PingtaiBean;
import com.kjsdfgk.hjmdsf.live.mvp.bean.VersionEntity;
import com.kjsdfgk.hjmdsf.live.mvp.ui.activity.GridLineItemDecoration;
import com.kjsdfgk.hjmdsf.live.mvp.ui.activity.LiveAdapter;
import com.kjsdfgk.hjmdsf.login.mvp.ui.activity.LoginActivity;
import com.kjsdfgk.hjmdsf.register.mvp.ui.activity.RegisterActivity;
import com.lksdkjs.live.R;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.tbruyelle.rxpermissions2.RxPermissions;


import java.util.List;

import butterknife.BindView;
import cn.leancloud.AVUser;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 04/30/2020 17:54
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class LiveV3FtFragment extends BaseFragment<LiveV3FtPresenter> implements LiveV3FtContract.View ,SwipeRefreshLayout.OnRefreshListener{

    public static LiveV3FtFragment newInstance() {
        LiveV3FtFragment fragment = new LiveV3FtFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerLiveV3FtComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_live_v3_ft, container, false);
    }
    @BindView(R.id.toolbar_title)
    TextView toolbar_title;
    @BindView(R.id.toolbar_back)
    RelativeLayout toolbar_back;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    RxPermissions mRxPermissions;
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        toolbar_title.setText(R.string.app_name);
        toolbar_back.setVisibility(View.INVISIBLE);

        mRxPermissions = new RxPermissions(this);
        mPresenter.getPingTaiJson();
        mPresenter.checkVersion();
        mSwipeRefreshLayout.setOnRefreshListener(this);
        final GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(), 3);
        mRecyclerView.setLayoutManager(mLayoutManager);
        GridLineItemDecoration mDecor = new GridLineItemDecoration(getActivity(), 0xFFe1e1e1, 3, 1);
        mRecyclerView.addItemDecoration(mDecor);
        ArmsUtils.configRecyclerView(mRecyclerView, mLayoutManager);

//        mPresenter.loginDevice();


    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading(String key) {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public boolean isCancel() {
        return false;
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    public void onRefresh() {
        mPresenter.getPingTaiJson();
    }

    LiveAdapter mAdapter;
    @Override
    public void showPingTaiJson(List<PingtaiBean> datas) {
        mRecyclerView.setAdapter(mAdapter = new LiveAdapter(datas));
    }

    @Override
    public void showNewVersion(VersionEntity updateVersion) {
        QMUIDialog.MessageDialogBuilder builder = new QMUIDialog.MessageDialogBuilder(getActivity());
        builder.setTitle("发现新版本--v" + updateVersion.getVersionName());
        builder.setMessage(updateVersion.getUpdateMessage());
        builder.addAction("立即更新", (dialog, index) -> {
//            showMessage("开始下载", TastyToast.INFO);
//            Intent intent = new Intent(MainActivity.this, UpdateDownloadService.class);
//            intent.putExtra("updateVersion", updateVersion);
//            startService(intent);
            Intent intent= new Intent();
            intent.setAction("android.intent.action.VIEW");
            Uri content_url = Uri.parse(updateVersion.getApkDownloadUrl());
            intent.setData(content_url);
            startActivity(intent);

        });
  /*      builder.addAction("稍后更新", (dialog, index) -> dialog.dismiss());
        builder.addAction("该版本不再提示", (dialog, index) -> {
            //保存版本号，用户对于此版本选择了不在提示
            presenter.setIgnoreUpdateVersionCode(updateVersion.getVersionCode());
            dialog.dismiss();
        });*/
        builder.setCancelable(false);
        builder.setCanceledOnTouchOutside(false);
        builder.show();
    }


    @Override
    public void showAccount(AVUser user) {

    }

    @Override
    public void showAccount(Account info) {

    }

    @Override
    public RxPermissions getRxPermissions() {
        return mRxPermissions;
    }

    @Override
    public void showLoginDlg() {
        QMUIDialog.MessageDialogBuilder builder = new QMUIDialog.MessageDialogBuilder(getActivity());
        builder.setTitle("系统提示");
        builder.setMessage("您尚未登录");

        builder.addAction("马上注册", (dialog, index) -> {
            dialog.cancel();
            Intent intent = new Intent(getActivity() , RegisterActivity.class);
            startActivity(intent);
        });
        builder.addAction("立即登录", (dialog, index) -> {
            dialog.cancel();
            Intent intent = new Intent(getActivity() , LoginActivity.class);
            startActivity(intent);

        });


        builder.show();
    }

    @Override
    public void showInitView() {

    }

    public void showMemberDlg() {
        QMUIDialog.MessageDialogBuilder builder = new QMUIDialog.MessageDialogBuilder(getActivity());
        builder.setTitle("系统提示");
        builder.setMessage("您的会员已过期 ，请续费");
        builder.addAction("     卡密购买     ", (dialog, index) -> {

            String url = SharedPrefsUtils.getString("kamiUrl","");
            if (!TextUtils.isEmpty(url)){
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
            }else {
                showMessage("系统错误 ，请稍后在试");
            }

        });
        builder.addAction("     卡密充值     ", (dialog, index) -> {
            Intent intent = new Intent(getActivity() , MemberChargeActivity.class);
            startActivity(intent);

        });
        builder.show();

    }
}
